package com.rabbitmq.demo.controller;

import com.rabbitmq.demo.conf.PublishSubscribeConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发布订阅模式生产者
 * @Author yi.cheng
 * @Date 2023-7-12 16:02
 **/
@RestController
public class PublishSubscribeProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/api/publish/sub")
    public String sendPubSubQueueMsg(@RequestParam(required = false) String message){
        if(StringUtils.isEmpty(message)){
            message = "this is simple publish subscribe message";
        }
        rabbitTemplate.convertAndSend(PublishSubscribeConfig.FANOUT_EXCHANGE_NAME, "",message.getBytes());
        return "ok";
    }
}
